<template>
  <div class="memory-game-container">
    <!-- 游戏头部 -->
    <div class="game-header">
      <h1>连连看小游戏</h1>
      <div class="game-stats">
        <div class="stat">
          <span class="stat-label">匹配进度：</span>
          <span class="stat-value">{{ matchedPairs }}/{{ values.length }}</span>
        </div>
        <div class="progress-bar">
          <div 
            class="progress-fill" 
            :style="{ width: `${(matchedPairs / values.length) * 100}%` }"
          ></div>
        </div>
      </div>
    </div>

    <!-- 游戏板 -->
    <div class="game-board">
      <div
        v-for="(card, index) in cards"
        :key="index"
        class="card"
        :class="{ 
          'flipped': card.flipped, 
          'matched': card.matched,
          'no-click': flippedCards.length === 2
        }"
        @click="flipCard(index)"
      >
        <div class="card-inner">
          <div class="card-front">
            <div class="card-pattern"></div>
          </div>
          <div class="card-back">
            {{ card.value }}
          </div>
        </div>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="action-buttons">
      <button class="btn btn-secondary" @click="goBack">
        <span>返回捏脸游戏</span>
      </button>
      <button class="btn btn-primary" @click="initGame">
        <span>重新开始</span>
      </button>
    </div>

    <!-- 通关提示 -->
    <transition name="bounce">
      <div v-if="matchedPairs === values.length" class="congrats-overlay">
        <div class="congrats-box">
          <h2>🎉 恭喜通关！ 🎉</h2>
          <p>你成功匹配了所有卡片！</p>
          <button class="btn btn-celebrate" @click="initGame">
            再玩一次
          </button>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'LianLianKan',
  data() {
    return {
      cards: [],
      flippedCards: [],
      matchedPairs: 0,
      values: ['🐶', '🐱', '🐭', '🐹', '🐰', '🦊', '🐻', '🐼']
    }
  },
  created() {
    this.initGame()
  },
  methods: {
    initGame() {
      // 创建卡片对并洗牌
      const pairs = [...this.values, ...this.values]
      this.cards = pairs
        .sort(() => Math.random() - 0.5)
        .map(value => ({
          value,
          flipped: false,
          matched: false
        }))
      this.flippedCards = []
      this.matchedPairs = 0
    },
    flipCard(index) {
      // 如果卡片已匹配或已翻开，或者已经有2张卡片被翻开，则忽略点击
      if (this.cards[index].matched || 
          this.cards[index].flipped || 
          this.flippedCards.length === 2) return

      // 翻开卡片
      this.cards[index].flipped = true
      this.flippedCards.push(index)

      // 检查是否匹配
      if (this.flippedCards.length === 2) {
        const [firstIndex, secondIndex] = this.flippedCards
        if (this.cards[firstIndex].value === this.cards[secondIndex].value) {
          // 匹配成功
          setTimeout(() => {
            this.cards[firstIndex].matched = true
            this.cards[secondIndex].matched = true
            this.matchedPairs++
            this.flippedCards = []
          }, 500)
        } else {
          // 不匹配，翻回去
          setTimeout(() => {
            this.cards[firstIndex].flipped = false
            this.cards[secondIndex].flipped = false
            this.flippedCards = []
          }, 1000)
        }
      }
    },
    goBack() {
      this.$emit('go-back')
    }
  }
}
</script>

<style scoped>
.memory-game-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Arial', sans-serif;
}

.game-header {
  text-align: center;
  margin-bottom: 30px;
}

.game-header h1 {
  color: #333;
  font-size: 2rem;
  margin-bottom: 15px;
}

.game-stats {
  background: #f8f8f8;
  border-radius: 10px;
  padding: 15px;
  margin-bottom: 20px;
}

.stat {
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
  font-size: 1.1rem;
}

.stat-value {
  color: #4CAF50;
  font-weight: bold;
}

.progress-bar {
  height: 10px;
  background: #e0e0e0;
  border-radius: 5px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(to right, #4CAF50, #8BC34A);
  transition: width 0.5s ease;
}

.game-board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px;
  perspective: 1000px;
}

.card {
  aspect-ratio: 1;
  cursor: pointer;
}

.card.no-click {
  cursor: not-allowed;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.6s;
  transform-style: preserve-3d;
}

.card.flipped .card-inner {
  transform: rotateY(180deg);
}

.card.matched .card-inner {
  transform: rotateY(180deg);
  cursor: default;
}

.card-front,
.card-back {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  border-radius: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.card-front {
  background: linear-gradient(135deg, #667eea, #764ba2);
  transform: rotateY(0deg);
}

.card-pattern {
  width: 80%;
  height: 80%;
  background: radial-gradient(circle, rgba(255,255,255,0.3) 0%, transparent 70%);
  border-radius: 50%;
}

.card-back {
  background: white;
  transform: rotateY(180deg);
  font-size: 2.5rem;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 30px;
}

.btn {
  padding: 12px 24px;
  border-radius: 30px;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s;
  border: none;
}

.btn-primary {
  background: linear-gradient(to right, #FF416C, #FF4B2B);
  color: white;
}

.btn-secondary {
  background: #f0f0f0;
  color: #333;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.congrats-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
}

.congrats-box {
  background: white;
  padding: 30px;
  border-radius: 15px;
  text-align: center;
  max-width: 80%;
}

.congrats-box h2 {
  color: #FF5722;
  margin-bottom: 15px;
}

.btn-celebrate {
  background: linear-gradient(to right, #FFC107, #FF9800);
  color: white;
  padding: 12px 24px;
  border-radius: 30px;
  border: none;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  margin-top: 15px;
}

.bounce-enter-active {
  animation: bounce-in 0.5s;
}
.bounce-leave-active {
  animation: bounce-in 0.5s reverse;
}
@keyframes bounce-in {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

@media (max-width: 600px) {
  .game-board {
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
  }
  
  .card-back {
    font-size: 2rem;
  }
}
</style>